package com.nit.lab.service.basic.impl;

import com.nit.lab.dto.basic.UnitDTO;
import com.nit.lab.dto.system.TreeDTO;
import com.nit.lab.dto.utils.PagingDTO;
import com.nit.lab.dto.utils.TableReqDTO;
import com.nit.lab.dto.utils.TableRspDTO;
import com.nit.lab.entity.basic.UnitEntity;
import com.nit.lab.mapper.basic.UnitMapper;
import com.nit.lab.service.basic.UnitService;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class UnitServiceImpl implements UnitService {
    @Autowired(required = false)
    private UnitMapper unitMapper;

    @Override
    public TableRspDTO list4Table(TableReqDTO tableReqDTO) throws Exception {
        Long count = unitMapper.count4Table(
                tableReqDTO.parseQueryParam("unitName"),
                tableReqDTO.parseQueryParam("unitFather"),
                tableReqDTO.parseQueryParam("unitType"));
        PagingDTO pagingDTO = new PagingDTO(
                tableReqDTO.getPageNo(),
                tableReqDTO.getPageSize(),
                count,
                unitMapper.list4Table(
                        tableReqDTO.getStart(),
                        tableReqDTO.getPageSize(),
                        tableReqDTO.parseQueryParam("unitName"),
                        tableReqDTO.parseQueryParam("unitFather"),
                        tableReqDTO.parseQueryParam("unitType"),
                        tableReqDTO.getSortField(),
                        tableReqDTO.getSortOrder()));
        TableRspDTO tableRspDTO = new TableRspDTO(pagingDTO);
        return tableRspDTO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer add(UnitDTO unitDTO) throws Exception {
        MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        UnitEntity unitEntity = mapperFactory.getMapperFacade().map(unitDTO, UnitEntity.class);
        unitEntity.setGmtCreate(new Date());
        return unitMapper.add(unitEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer update(UnitDTO unitDTO) throws Exception {
        MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        UnitEntity unitEntity = mapperFactory.getMapperFacade().map(unitDTO, UnitEntity.class);
        return unitMapper.update(unitEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer remove(String unitIds) throws Exception {
        String[] arrayUnitId = unitIds.split(",");
        for (int i = 0; i < arrayUnitId.length; i++) {
            unitMapper.remove(Long.parseLong(arrayUnitId[i]));
        }
        return arrayUnitId.length;
    }

    @Override
    public List<UnitDTO> listUnit() throws Exception{
        return unitMapper.listUnit();
    }

    @Override
    public List<TreeDTO> listUnitTree(Long unitId, Long unitType) throws Exception {
        TreeDTO treeDto;
        List<TreeDTO> listTreeTwo;
        List<TreeDTO> listTreeThree;
        List<TreeDTO> listTreeOne = new ArrayList<TreeDTO>();
        List<UnitDTO> listUnitDto = unitMapper.listUnitTree();
        listTreeTwo = new ArrayList<TreeDTO>();
        //通过单位fatherId判断,生成单位树型结构
        for (int j = 1; j < listUnitDto.size(); j++) {
            if (listUnitDto.get(j).getActions().equals(String.valueOf(unitId))) {
                listTreeThree = new ArrayList<TreeDTO>();
                if (unitType == 1) { //一级单位生成三级树
                    for (int k = 1; k < listUnitDto.size(); k++) {
                        if (listUnitDto.get(k).getActions().equals(String.valueOf(listUnitDto.get(j).getUnitId()))) {
                            treeDto = new TreeDTO();
                            treeDto.setTitle(listUnitDto.get(k).getUnitName());
                            treeDto.setKey(Integer.parseInt(String.valueOf(listUnitDto.get(k).getUnitId())));
                            treeDto.setValue(Integer.parseInt(String.valueOf(listUnitDto.get(k).getUnitId())));
                            listTreeThree.add(treeDto);
                        }
                    }
                }
                treeDto = new TreeDTO();
                treeDto.setTitle(listUnitDto.get(j).getUnitName());
                treeDto.setKey(Integer.parseInt(String.valueOf(listUnitDto.get(j).getUnitId())));
                treeDto.setValue(Integer.parseInt(String.valueOf(listUnitDto.get(j).getUnitId())));
                treeDto.setChildren(listTreeThree);
                listTreeTwo.add(treeDto);
            }
        }
        for (UnitDTO unitDTO : listUnitDto) {
            if (unitDTO.getUnitId().equals(unitId)) {
                treeDto = new TreeDTO();
                treeDto.setTitle(unitDTO.getUnitName());
                treeDto.setKey(Integer.parseInt(String.valueOf(unitDTO.getUnitId())));
                treeDto.setValue(Integer.parseInt(String.valueOf(unitDTO.getUnitId())));
                treeDto.setChildren(listTreeTwo);
                listTreeOne.add(treeDto);
                break;
            }
        }
        return listTreeOne;
    }

    @Override
    public UnitDTO getUnitByInfo(String unitInfo) throws Exception{
        return unitMapper.getUnitByInfo(unitInfo);
    }

    @Override
    public UnitDTO getUnitById(Long unitId) throws Exception{
        return unitMapper.getUnitById(unitId);
    }

    @Override
    public List<UnitDTO> listUnitByUnitName(String unitName) throws Exception{
        return unitMapper.listUnitByUnitName(unitName);
    }

    @Override
    public String getUnitNameById(Long unitId) throws Exception {
        return unitMapper.getUnitNameById(unitId);
    }
}
